package com.itheima.train.manager.order.handler.configuration;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitmqConfiguration {

    // 声明队列
    @Bean(name = "back_stock_redis_queue")
    public Queue backStockRedisQueue() {
        return QueueBuilder.durable("back_stock_redis_queue").build() ;
    }

    // 声明队列
    @Bean(name = "back_stock_es_queue")
    public Queue backStockESQueue() {
        return QueueBuilder.durable("back_stock_es_queue").build() ;
    }

    // 声明交换机和队列的绑定关系
    @Bean
    public Binding trainExBindBackStockRedisQueue(@Qualifier(value = "train_manager_ex") Exchange exchange , @Qualifier(value = "back_stock_redis_queue") Queue queue) {
        return BindingBuilder.bind(queue).to(exchange).with("back_stock").noargs() ;
    }

    // 声明交换机和队列的绑定关系
    @Bean
    public Binding trainExBindBackStockESQueue(@Qualifier(value = "train_manager_ex") Exchange exchange , @Qualifier(value = "back_stock_es_queue") Queue queue) {
        return BindingBuilder.bind(queue).to(exchange).with("back_stock").noargs() ;
    }

    // 声明回退库存的死信交换机
    @Bean(name = "order_timeout_dlx_ex")
    public Exchange backStockDlxExchange() {
        return ExchangeBuilder.directExchange("order_timeout_dlx_ex").durable(true).build() ;
    }

    // 声明回退库存的死信队列
    @Bean(name = "order_timeout_dlx_queue")
    public Queue backStockDlxQueue() {
        return QueueBuilder.durable("order_timeout_dlx_queue").build() ;
    }

    // 声明回退库存的队列
    @Bean(name = "order_timeout_queue")
    public Queue orderTimeOutQueue() {
        QueueBuilder queueBuilder = QueueBuilder.durable("order_timeout_queue");        // 订单队列建造者对象
        queueBuilder.ttl(1000 * 60);                                                    // 单位为毫秒
        queueBuilder.deadLetterExchange("order_timeout_dlx_ex") ;                       // 设置死信交换机
        queueBuilder.deadLetterRoutingKey("order_timeout") ;                            // 死信交换机路由键
        return queueBuilder.build() ;
    }

    // 声明交换机和队列的绑定关系
    @Bean
    public Binding trainExBindOrderTimeOutQueue(@Qualifier(value = "train_manager_ex") Exchange exchange , @Qualifier(value = "order_timeout_queue") Queue queue) {
        return BindingBuilder.bind(queue).to(exchange).with("order_timeout").noargs() ;
    }

    // 声明交换机和队列的绑定关系
    @Bean
    public Binding backStockDlxExBindBackStockDlxQueue(@Qualifier(value = "order_timeout_dlx_ex") Exchange exchange , @Qualifier(value = "order_timeout_dlx_queue") Queue queue) {
        return BindingBuilder.bind(queue).to(exchange).with("order_timeout").noargs() ;
    }

    // 从Redis中删除排队信息的的队列
    @Bean(name = "delete_sorted_from_redis")
    public Queue deleteSortedFromRedisQueue() {
        return QueueBuilder.durable("delete_sorted_from_redis").build() ;
    }

    // 声明队列
    @Bean(name = "syn_gen_order_queue")
    public Queue genOrderQueue() {
        return QueueBuilder.durable("syn_gen_order_queue").build() ;
    }

    // 声明交换机
    @Bean(name = "train_manager_ex")
    public Exchange trainManagerExchange() {
        return ExchangeBuilder.directExchange("train_manager_ex").durable(true).build() ;
    }

    // 声明队列
    @Bean(name = "syn_seat_to_redis_queue")
    public Queue synSeatToRedisQueue() {
        return QueueBuilder.durable("syn_seat_to_redis_queue").build() ;
    }

    @Bean(name = "syn_riding_plan_to_redis_queue")
    public Queue synRidingPlanToRedisQueue() {
        return QueueBuilder.durable("syn_riding_plan_to_redis_queue").build() ;
    }

    @Bean(name = "syn_riding_plan_to_es_queue")
    public Queue synRidingPlanToESQueue() {
        return QueueBuilder.durable("syn_riding_plan_to_es_queue").build() ;
    }

    // 完成队列和交换机的绑定
    @Bean
    public Binding trainManagerExBindingSeatRedisQueue(@Qualifier(value = "train_manager_ex") Exchange exchange , @Qualifier(value = "syn_seat_to_redis_queue") Queue queue) {
        return BindingBuilder.bind(queue).to(exchange).with("syn_seat").noargs() ;
    }

    @Bean
    public Binding trainManagerExBindingRidingPlanRedisQueue(@Qualifier(value = "train_manager_ex") Exchange exchange , @Qualifier(value = "syn_riding_plan_to_redis_queue") Queue queue) {
        return BindingBuilder.bind(queue).to(exchange).with("syn_riding_plan").noargs() ;
    }

    @Bean
    public Binding trainManagerExBindingRidingPlanESQueue(@Qualifier(value = "train_manager_ex") Exchange exchange , @Qualifier(value = "syn_riding_plan_to_es_queue") Queue queue) {
        return BindingBuilder.bind(queue).to(exchange).with("syn_riding_plan").noargs() ;
    }

    // 声明交换机和队列的绑定关系
    @Bean
    public Binding trainExBindGenOrderQueue(@Qualifier(value = "train_manager_ex") Exchange exchange , @Qualifier(value = "syn_gen_order_queue") Queue queue) {
        return BindingBuilder.bind(queue).to(exchange).with("gen_order").noargs() ;
    }

    // 声明交换机和队列的绑定关系
    @Bean
    public Binding trainExBinddeleteSortedFromRedisQueue(@Qualifier(value = "train_manager_ex") Exchange exchange , @Qualifier(value = "delete_sorted_from_redis") Queue queue) {
        return BindingBuilder.bind(queue).to(exchange).with("delete_sorted").noargs() ;
    }

}
